package com.bkhc.umc.service;

import com.bkhc.common.util.CommonUtil;
import com.bkhc.common.util.MyBeanUtils;
import com.bkhc.common.util.response.ResultCode;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.UserBean;
import com.bkhc.common.vo.UserRoleVo;
import com.bkhc.common.vo.UserVo;
import com.bkhc.umc.entity.Address;
import com.bkhc.umc.entity.User;
import com.bkhc.umc.entity.UserRole;
import com.bkhc.umc.repository.*;
import com.bkhc.umc.vo.ApplyInfomation;
import com.bkhc.umc.vo.Customer;
import com.bkhc.umc.vo.UserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class AddressSerivce {

    @Autowired
    private UserRepository repository;
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private UserRoleRepository userRoleRepository;
    @Autowired
    private BusinessRepository businessRepository;
    @Autowired
    private AddressRepository addressRepository;


    public ResultJson<?> queryAddressByUserId(String userId) {
        List<Address> addressList = addressRepository.findByUserId(userId);
        ResultJson<List<Address> > resultJson = ResultJson.success();
        resultJson.setData(addressList);
        return resultJson;
    }

    public ResultJson<?> updateAddress(Address address) {
        addressRepository.save(address);
        ResultJson<?> resultJson = ResultJson.success();
        return resultJson;
    }

    public ResultJson<?> updateDefaultAddress(Address address) {
        List<Address> addressList = addressRepository.findByUserId(address.getUserId());
        if(!CollectionUtils.isEmpty(addressList)){
            for(Address entity:addressList){
                if(entity.getId()==address.getId()){
                    entity.setDefaultFlag(true);
                }else{
                    entity.setDefaultFlag(false);
                }
            }
            addressRepository.saveAll(addressList);
        }
        ResultJson<?> resultJson = ResultJson.success();
        return resultJson;
    }

    public ResultJson<?> addAddress(Address address) {
        addressRepository.save(address);
        ResultJson<?> resultJson = ResultJson.success();
        return resultJson;
    }

    public ResultJson<?> deleteAddress(Address address) {
        addressRepository.delete(address);
        ResultJson<?> resultJson = ResultJson.success();
        return resultJson;
    }
}
